<?php

class Edit extends TPage
{
	private $element_manager;
	private $element;
	
	public function onLoad($param)
	{
		parent::onLoad($param);
		$this->element_manager = $this->Application->Modules['requested_elements'];
		
		$this->element = $this->element_manager->getRequestedElementById($this->Request['id']);
		if(!($this->element instanceof RequestedElementRecord))
			$this->Response->redirect($this->Service->constructUrl('committee.requested_elements.All'));
			
		if(!$this->IsPostback)
		{
			$this->readData();
		}
	}
	
	public function readData()
	{
		$this->name->Text = $this->element->getName();
		$this->description_checked->Checked = $this->element->getDescription() !== null;
		$this->description->Text = $this->element->getDescription() !== null ? $this->element->getDescription() : '';
				
		$user_manager = $this->Application->Modules['users'];
		$user = $user_manager->getUserById($this->element->getOwnerId());
		$this->owner->Text = $user !== null ?$user->getFullName() : '-';
		
		$element_manager = $this->Application->Modules['conference_elements'];
		$conference_element_list = array();
		
		foreach($element_manager->getConferenceElementsListByIsSession() as $conference_element)
			$conference_element_list[] = array(	'id' => $conference_element->getId(),
												'name' => $conference_element->getName());
												
		$this->session_list->DataValueField = 'id';
		$this->session_list->DataTextField  = 'name';
		$this->session_list->DataSource = $conference_element_list;
		$this->session_list->dataBind();

		if(sizeof($conference_element_list) == 0)
			$this->accepted_status->Enabled = false;
		
		if($this->element->getStatus() == StatusManager::$ACCEPTED_STATUS)
		{
			$this->accepted_status->Checked = true;
			$this->session_list->setSelectedValue($this->element->getSessionId());
		}
		elseif($this->element->getStatus() == StatusManager::$REJECTED_STATUS)
			$this->rejected_status->Checked = true;
		else
			$this->awaiting_status->Checked = true;
	}
	
	public function saveButtonClicked($sender, $params)
	{
		if($this->Page->IsValid)
		{
			$this->element->setName(String::format($this->name->Text));
			$this->element->setDescription($this->description_checked->Checked ? String::format($this->description->Text) : null);
			
			if($this->accepted_status->Checked)
			{
				$this->element->setStatus(StatusManager::$ACCEPTED_STATUS);
				$this->element->setSessionId($this->session_list->getSelectedValue());
			}
			if($this->rejected_status->Checked)
			{
				$this->element->setStatus(StatusManager::$REJECTED_STATUS);
				$this->element->setSessionId(null);
			}
			if($this->awaiting_status->Checked)
			{
				$this->element->setStatus(StatusManager::$AWAITING_STATUS);
				$this->element->setSessionId(null);
			}
			
			
			$result = $this->element_manager->updateRequestedElementByRequestedElementRecord(
													$this->element
												);
			
			if($result)
			{
				$this->info->Text = '<p>'.Prado::localize('Changes saved').'</p>';
			}
			else
				$this->info->Text = '<p>'.Prado::localize('Changes not saved').'</p>';
		}
	}	
}

?>